<?php
	session_start();
	require_once('../inc/fnc.php');
	require_once('../inc/conn.php');
	require_once('../inc/settings.php');
	
	// Initializing variables
	$uniqueid = $_POST['uniqueid'];
	$storyId = $_POST['storyid'];
	$vote = $_POST['flag'];
	$userId = $_SESSION['userId'];
	
	//echo $uniqueid . " " . $storyId . " " . $vote . " " . $userId;
	$val = 0;
	$query = "SELECT * FROM flag where uniqueentrylineid = '{$uniqueid}' and flaggivenid = '{$userId}' and delflag = 'N' LIMIT 1";
	$result = mysql_query($query) or die(mysql_error());
	
	if(mysql_num_rows($result) == 1) { $val = 1; }
	if($val == 0) {
		$query = "SELECT * FROM entryline where displayseq = '{$uniqueid}' and delflag = 'N'";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)) {
			$query = "insert into flag (uniqueentrylineid,storyid,linecrtdbyid,flaggivenid,delflag,ctrdtime) values
										({$uniqueid},{$storyId},{$row['crtdbyid']},{$userId},'N',NOW())";
			$return = mysql_query($query);
		}
		$return = 1;
		$query = "SELECT count(storyid) as noflag FROM flag WHERE uniqueentrylineid = {$uniqueid} AND delflag = 'N'";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)) {
			if($row['noflag'] > 9) {
				mysql_query("update entryline set delflag = 'Y' WHERE displayseq = '{$uniqueid}' and delflag = 'N'");
				mysql_query("update entrylinevote set delflag = 'Y' WHERE entrylineid = '{$uniqueid}' and delflag = 'N'");
			}
			$return = 2;
		}
	} else {
		$return = 3;
	}
    echo $return;    
?>
